/*$(function() {
	var sortTypeId = getvl("sortTypeId");
	if(sortTypeId != '') {
		searchBySortType(sortTypeId);
	}
})*/

//商圈
TradeArea();
//分类
sortType();
//属性
propertyType();

var currentPage = 1;
var tradeAreaId = '';
var sortTypeId = '';
var propertyTypeId = '';
var tradeNumber = '',
	goodsType = '',
	isReturnedOfSevenDays = '';
var sort = ''; //排序
//价格区间
var betweenMoney = '';
var goldRate = 1;

//列表
loadMyEssay(tradeAreaId, sortTypeId, propertyTypeId, tradeNumber, goodsType, isReturnedOfSevenDays, sort, betweenMoney);

function loadMyEssay(tradeAreaId, sortTypeId, propertyTypeId, tradeNumber, goodsType, isReturnedOfSevenDays, sort, betweenMoney) {
	$(document).ready(function() {
		init(currentPage);
	});

	function init(pageNo) {
		$("#goldRateList").html("");
		$.ajax({
			url: urlcore + "/api/goods/selectGoodsList?tradeAreaId=" + tradeAreaId + "&sortTypeId=" + sortTypeId + "&propertyTypeId=" + propertyTypeId + "&tradeNumber=" + tradeNumber + "&goodsType=" + goodsType + "&isReturnedOfSevenDays=" + isReturnedOfSevenDays + "&sort=" + sort + "&betweenMoney=" + betweenMoney + "&goldRate=" + 1 + "&size=" + 32 + "&current=" + pageNo,
			type: "get",
			dataType: 'json',
			async: false,
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					$.each(data.data.list, function(i, n) {
						
						var coverUrl=n.coverUrl==null?'http://app.jiagangwangluo.com/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/default.png':n.coverUrl;
						coverUrl=isHasImg(coverUrl);
						var goodsTitle=n.goodsTitle==null?'暂无':n.goodsTitle;
						var tradePrice=n.tradePrice==null?'0':n.tradePrice;
						var goldRate=n.goldRate==null?'0':n.goldRate;
						var collectNum=n.collectNum==null?'0':n.collectNum;
						
						var thislist =
							'<dl>' +
							'<a target="_blank" href="2.2.0goods_detail.html?id='+n.id+'&breadcrumbType=3">' +
							'<dt>' +
							'<img class="searchGoodsByJinbiImg" src="' + coverUrl + '" />' +
							'</dt>' +
							'<dd>' +
							'<h5>' + goodsTitle + '</h5>' +
							'<h6>&yen;' + tradePrice + '<em>&nbsp;金币可抵' + goldRate + '%</em><span>收藏' + collectNum + '</span></h6>' +
							'</dd>' +
							'</a>' +
							'</dl>';
						$('#goldRateList').append(thislist);
					});

                    //固定宽高
					$(function(){
						var imgList1 = document.getElementsByClassName("searchGoodsByJinbiImg");  
					    var w=290;
					    var h=290;
					    for(var i=0;i<imgList1.length;i++){
					        if(imgList1[i].width>w || imgList1[i].height>h){  
					            imgList1[i].width = w;  
					            imgList1[i].heigth = h;  
					        }  
					    }  
					})

					$("#pager").pager({
						pagenumber: pageNo,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});

				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					//alert(thisUrl);
					if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
						top.window.location.href = "1.0.0denglu.html";
					}

				} else {
					if(data.msg != '空数据') {
						layer.alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}

			},
			error: function() {
				layer.alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}
}

/*function TradeArea() {
	$("#tradeArea").html("");
	$.ajax({
		url: urlcore + "/api/tradeArea/selectList",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				$.each(data.data, function(i, n) {
					var thislist = '<a href="javascript:searchByTradeArea(' + n.id + ');">' + '<span class="tags">' + n.areaName + '</span>' + '</a>';
					$('#tradeArea').append(thislist);
				});
			}
		},
		error: function() {
			alert("error");
		}
	});
}*/
//按区域查询
function searchByTradeArea(tradeAreaId) {
	loadMyEssay(tradeAreaId, '', '', '', '', '', '', '');
}

/*function sortType() {
	$("#sortType").html("");
	$.ajax({
		url: urlcore + "/api/sortType/allSortTypeList",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {

			if(data.success == true) {
				$.each(data.data, function(i, n) {
					var thislist = '<a href="javascript:searchBySortType(' + n.id + ');">' + '<span class="tags">' + n.sortName + '</span>' + '</a>';
					$('#sortType').append(thislist);
				});
			}
		},
		error: function() {
			alert("error");
		}
	});
}*/
//按分类查询
function searchBySortType(sortTypeId) {
	$.ajax({
		url: urlcore + "/api/sortType/selectOne?id=" + sortTypeId,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				$("#breadcrumbName").text(data.data.sortName);
			}
		},
		error: function() {
			layer.alert("error");
		}
	});

	loadMyEssay('', sortTypeId, '', '', '', '', '', '');
}

/*function propertyType() {
	$("#property").html("");
	$.ajax({
		url: urlcore + "/api/propertyType/selectTwoList",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {

			if(data.success == true) {
				$.each(data.data.list, function(i, n) {
					var erji = '';
					$.each(n.propertyTypeList, function(i, m) {
						erji += '<a href="javascript:searchByPropertyType(' + m.id + ');">' + '<span data-type="attr" class="search_conditions tags" aid="' + m.oneTypeId + '">' + m.propertyName + '</span>' + '</a>';
					});

					var thislist = '<div class="tattr" id="tattr123">' +
						'<h5 class="tattr-hd">' + n.propertyName + '<i class="icon icon-arrup"></i>' +
						'</h5>' +
						'<p class="tattr-list">' + erji + '</p>' +
						'</div>';
					$('#property').append(thislist);
				});
				//属性js操作
				$('.tattr').on('mouseenter', function() {

					var _this = $(this),
						icon = _this.find('.icon'),
						list = _this.find('.tattr-list');
					icon.addClass('icon-arrdown');

					list.show();
				}).on('mouseleave', function() {
					var _this = $(this),
						icon = _this.find('.icon'),
						list = _this.find('.tattr-list');
					icon.removeClass('icon-arrdown');
					list.hide();
				});
				//属性选择
				$('.tattr-list').on('click', '.tags', function() {
					var _this = $(this),
						html = '<span class="tags">' + _this.text() + ' <span class="close1">X</span></span>',
						parent = _this.parents('.tablist');
					$('#tag').html(html);
					parent.hide();
				});
				// 选择属性删除
				$('#tag').on('click', function() {
					$("#tag .tags").remove();
					parent = $('.tattr-list').parents('.tablist');
					parent.show();
					location.reload();
				});
			}
		},
		error: function() {
			alert("error");
		}
	});
}*/
//按属性查询
function searchByPropertyType(propertyTypeId) {
	loadMyEssay('', '', propertyTypeId, '', '', '', '', '');
}

//搜索 一件起批   热销商品   支持包换
function selectGoods(tradeNumber, goodsType, isReturnedOfSevenDays) {

	loadMyEssay('', '', '', tradeNumber, goodsType, isReturnedOfSevenDays, '', '');
}
//按时间,单价,收藏量,销售量排序
function selectGoodsOrderBySort(sort) {
	loadMyEssay('', '', '', '', '', '', sort, '');
}
//按价格区间以及起批量查询 
function selectGoodsByMoneyOrTradeNumber() {
	var minPprice = $("#min_price").val().trim();
	var maxPrice = $("#max_price").val().trim();
	var betweenMoney = '';
	if(minPprice != '' && minPprice != null && maxPrice != '' && maxPrice != null) {
		betweenMoney = minPprice + "-" + maxPrice;
	}
	var retailPum = $("#retail_num").val().trim();

	loadMyEssay('', '', '', retailPum, '', '', '', betweenMoney);
}


